/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @BaBaseFragment.java  2014-9-9 上午10:50:49 - Carson
 * @author YanXu
 * @email:981385016@qq.com
 * @version 1.0
 */

package com.android.tiango.baba;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.view.AppTitleBar;
import com.android.tiango.baba.view.WaitingDialog;

public class BaBaseFragment extends Fragment {
	// protected RelativeLayout mLoading;
	protected AppTitleBar mTitleBar;
	private WaitingDialog mWaitingDialog;

	// @Override
	// public void onStart() {
	// super.onStart();
	// setBackOnClickListener();
	// }

	protected void setBackOnClickListener() {
		if (mTitleBar != null) {
			mTitleBar.setBackOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					getActivity().onBackPressed();
				}
			});
		}
	}

	/**
	 * 根据id获取string中的数据
	 * @param id
	 * @return
	 */
	protected String getStringValue(int id) {
		String value = BaBaseApplication.getAppContext().getResources().getString(id);
		return StringUtils.isEmpty(value) ? "" : value;
	}

	protected void showActivity(Class<?> classz) {
		try {
			startActivity(new Intent(getActivity(), classz));
		} catch (Exception e) {
			Log.e("ss", e.getMessage());
		}

	}

	public void showActivity(Class<?> classz, String data) {
		Intent it = new Intent(getActivity(), classz);
		it.putExtra(Constants.DATA, data);
		startActivity(it);
	}

	public void showWaitingDialog() {
		if (mWaitingDialog == null) {
			mWaitingDialog = new WaitingDialog(getActivity());
			// mWaitingDialog = new ProgressDialog(this);
			mWaitingDialog.setCanceledOnTouchOutside(false);
			// mWaitingDialog.setMessage(getString(R.string.action_waiting));
			mWaitingDialog.setCancelable(true);
		}
		if (!getActivity().isFinishing() && !mWaitingDialog.isShowing()) {
			mWaitingDialog.show();
		}
	}

	public void showWaitingDialog(String msg) {
		if (mWaitingDialog == null) {
			mWaitingDialog = new WaitingDialog(getActivity());
			// mWaitingDialog = new ProgressDialog(this);
			mWaitingDialog.setCanceledOnTouchOutside(false);
			// mWaitingDialog.setMessage(getString(R.string.action_waiting));
			mWaitingDialog.setCancelable(true);
		}
		mWaitingDialog.setMessage(msg);
		if (!getActivity().isFinishing() && !mWaitingDialog.isShowing()) {
			mWaitingDialog.show();
		}
	}

	public void dismissWaitingDialog() {
		if (mWaitingDialog != null && mWaitingDialog.isShowing() && !getActivity().isFinishing()) {
			mWaitingDialog.dismiss();
		}
	}
}
